package com.xpoint.backend.controller;

import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.AddressBook;
import com.xpoint.backend.entity.ShoppingCart;
import com.xpoint.backend.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Cacheable(value = "ShoppingCart")
    @RequestMapping("/list")
//    查询购物车中的菜单
    public R<List<ShoppingCart>> findList(){
        return shoppingCartService.findList();
    }
    @PostMapping("/add")
    @CacheEvict(value = "ShoppingCart",allEntries = true)
//    添加到购物车
    public R<ShoppingCart> addShopping(@RequestBody ShoppingCart shoppingCart){
        return shoppingCartService.addSoppingCart(shoppingCart);
    }

    @PostMapping("/sub")
    @CacheEvict(value = "ShoppingCart",allEntries = true)
//    下单数量-1
    public R<ShoppingCart> subShopping(@RequestBody ShoppingCart shoppingCart){
        System.out.println(shoppingCart);
        return shoppingCartService.subShopping(shoppingCart);
    }

    //    清除购物车
    @DeleteMapping("clean")
    @CacheEvict(value = "ShoppingCart",allEntries = true)
    public R<String> cleanShoppingCart(){
        return shoppingCartService.removeShoppingCart();
    }
}
